package guowei.guliorder.order.listener;

import com.rabbitmq.client.Channel;
import guowei.guliorder.order.entity.OrderEntity;
import guowei.guliorder.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;

@Service
@RabbitListener(queues = "order.release.order.queue")
public class orderListener {


    @Autowired
    OrderService orderService;

    /**
     * 监听普通队列，只要接收到了消息，就说明订单过期
     *  并手动签收消息，避免留在队列里
     * @param orderEntity
     */
    @RabbitHandler
    public void listenReleaseQueue(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        try {
            System.out.println("收到过期的订单消息，检查订单状态"+orderEntity.getOrderSn());
            orderService.rollbackMq(orderEntity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}